# -*- coding: utf-8 -*-
"""
#PBAR_ExamineSpectraShapeAcrossDetectors.py

Created on Thu Oct 31 10:58:31 2013

@author: jkwong


Run this after running PBAR_Zspec_DetermineBestAlgorithm.py which will load all the data, etc.
Very clunky.

"""

# calculate the discrminiant
(wBest, pfitmean, pfitsigma) = PBAR_Zspec.GetZspecDiscrimValues()
stats['dist0']  = stats['binMean_g1'] * wBest[0] + stats['binSTD_g1'] * wBest[1] + stats['multibin_20_ratio_g1'] * wBest[2]
stats['discrim'] =  stats['dist0'] - np.polyval(pfitmean, stats['count'])


# diagnostic scatter plot
plt.figure()
plt.grid()
cut = goodZspecMask
index= 1
plt.plot(stats['count'][index,cut], stats['discrim'][index,cut], '.k')

index= 2
plt.plot(stats['count'][index,cut], stats['discrim'][index,cut], 'dk')

index= 5
plt.plot(stats['count'][index,cut], stats['discrim'][index,cut], '.r')
index= 6
plt.plot(stats['count'][index,cut], stats['discrim'][index,cut], 'dr')


index= 8
plt.plot(stats['count'][index,cut], stats['discrim'][index,cut], '.g')
index= 9
plt.plot(stats['count'][index,cut], stats['discrim'][index,cut], 'dg')


plt.ylim([-30, 30])




# Features and discriminant vs detector number for good detectors
datasetIndex = 11
featureNameList = ['binMean_g1', 'binSTD_g1', 'multibin_20_ratio_g1', 'discrim']
featureNameList = ['binMean', 'binSTD', 'multibin_20_ratio', 'discrim']

f, axarr = plt.subplots(2, 2)
cut = goodZspecMask

axarr = axarr.flatten()

for (featureNameIndex, featureName) in enumerate(featureNameList):
    axarr[featureNameIndex].plot(np.arange(1,137)[cut], stats[featureName][datasetIndex,cut], '-xk')
    axarr[featureNameIndex].grid()
    axarr[featureNameIndex].set_xlabel('Detector Number')
    axarr[featureNameIndex].set_ylabel(featureName)
    if featureNameIndex == 0:
        axarr[featureNameIndex].set_title(datasetDescription[datasetIndex])


# Plot of groups of detectors near the center/sweet spot of the beam
index = 0

#detectorList = [16, 17, 18, 19] # top
#detectorList = [20, 21, 22, 23] # top
#detectorList = [108, 109, 110, 111] # bottom
detectorList = [64, 65, 66, 67, 68, 69, 70]

plt.figure()
plt.grid()
for detectorIndex in detectorList:
    plt.plot(dat[index][:,detectorIndex]/300./60., label = '%d' %(detectorIndex+1))

plt.legend()
plt.xlabel('Bin');
plt.ylabel('Count/x-ray pulse')

plt.yscale('log')
plt.axis((0, 150, 1e-5, 20))

#

# Plot of groups of detectors near the center/sweet spot of the beam
index = 0

#detectorList = [16, 17, 18, 19] # top
detectorList = [20, 21, 22, 23] # top
#detectorList = [108, 109, 110, 111] # bottom
#detectorList = [64, 65, 66, 67, 68, 69, 70] # middle

plt.figure()
plt.grid()
for detectorIndex in detectorList:
    plt.plot(np.arange(256), dat[index][:,detectorIndex]/300./60., '-k', label = '%d' %(detectorIndex+1))

#detectorList = [16, 17, 18, 19] # top
#detectorList = [20, 21, 22, 23] # top
#detectorList = [108, 109, 110, 111] # bottom
detectorList = [64, 65, 66, 67, 68, 69, 70] # middle

for detectorIndex in detectorList:
    plt.plot(np.arange(256), dat[index][:,detectorIndex]/300./60., '-r', label = '%d' %(detectorIndex+1))


#detectorList = [16, 17, 18, 19] # top
#detectorList = [20, 21, 22, 23] # top
detectorList = [108, 109, 110, 111] # bottom
#detectorList = [64, 65, 66, 67, 68, 69, 70] # middle

for detectorIndex in detectorList:
    plt.plot(np.arange(256), dat[index][:,detectorIndex]/300./60., '-b', label = '%d' %(detectorIndex+1))

plt.legend()
plt.xlabel('Bin');
plt.ylabel('Count/x-ray pulse')

plt.yscale('log')
plt.axis((0, 150, 1e-5, 20))


# with gain correction to bin number

# Plot of groups of detectors near the center/sweet spot of the beam
index = 0

#detectorList = [16, 17, 18, 19] # top
detectorList = [20, 21, 22, 23] # top
#detectorList = [108, 109, 110, 111] # bottom
#detectorList = [64, 65, 66, 67, 68, 69, 70] # middle

plt.figure()
plt.grid()
for detectorIndex in detectorList:
    plt.plot(np.arange(256)*gainCorrection[index, detectorIndex], dat[index][:,detectorIndex]/300./60., '-k', label = '%d' %(detectorIndex+1))

#detectorList = [16, 17, 18, 19] # top
#detectorList = [20, 21, 22, 23] # top
#detectorList = [108, 109, 110, 111] # bottom
detectorList = [64, 65, 66, 67, 68, 69, 70] # middle

for detectorIndex in detectorList:
    plt.plot(np.arange(256)*gainCorrection[index, detectorIndex], dat[index][:,detectorIndex]/300./60., '-r', label = '%d' %(detectorIndex+1))


#detectorList = [16, 17, 18, 19] # top
#detectorList = [20, 21, 22, 23] # top
detectorList = [108, 109, 110, 111] # bottom
#detectorList = [64, 65, 66, 67, 68, 69, 70] # middle

for detectorIndex in detectorList:
    plt.plot(np.arange(256)*gainCorrection[index, detectorIndex], dat[index][:,detectorIndex]/300./60., '-b', label = '%d' %(detectorIndex+1))

plt.legend()
plt.xlabel('Bin');
plt.ylabel('Count/x-ray pulse')

plt.yscale('log')
plt.axis((0, 150, 1e-5, 20))

# with gain correction and amplitude adjustment


# Plot of groups of detectors near the center/sweet spot of the beam
index = 0

#detectorList = [16, 17, 18, 19] # top
detectorList = [20, 21, 22, 23] # top
#detectorList = [108, 109, 110, 111] # bottom
#detectorList = [64, 65, 66, 67, 68, 69, 70] # middle

plt.figure()
plt.grid()
for detectorIndex in detectorList:
    plt.plot(np.arange(256)*gainCorrection[index, detectorIndex], \
    dat[index][:,detectorIndex]/300./60./gainCorrection[index, detectorIndex], '-k', label = '%d' %(detectorIndex+1))

#detectorList = [16, 17, 18, 19] # top
#detectorList = [20, 21, 22, 23] # top
#detectorList = [108, 109, 110, 111] # bottom
detectorList = [64, 65, 66, 67, 68, 69, 70] # middle

for detectorIndex in detectorList:
    plt.plot(np.arange(256)*gainCorrection[index, detectorIndex], \
    dat[index][:,detectorIndex]/300./60./gainCorrection[index, detectorIndex], '-r', label = '%d' %(detectorIndex+1))


#detectorList = [16, 17, 18, 19] # top
#detectorList = [20, 21, 22, 23] # top
detectorList = [108, 109, 110, 111] # bottom
#detectorList = [64, 65, 66, 67, 68, 69, 70] # middle

for detectorIndex in detectorList:
    plt.plot(np.arange(256)*gainCorrection[index, detectorIndex], \
    dat[index][:,detectorIndex]/300./60./gainCorrection[index, detectorIndex], '-b', label = '%d' %(detectorIndex+1))

plt.legend()
plt.xlabel('Bin');
plt.ylabel('Count/x-ray pulse')

plt.yscale('log')
plt.axis((0, 150, 1e-5, 20))





# Plot of groups of detectors near the center/sweet spot of the beam
index = 0

detectorList = [16, 17, 18, 19] # top
detectorList = [20, 21, 22, 23] # top
#detectorList = [108, 109, 110, 111] # bottom
#detectorList = [64, 65, 66, 67, 68, 69, 70]

plt.figure()
plt.grid()
for detectorIndex in detectorList:
    plt.plot((dat[index][:,detectorIndex] - dat[detectorList[0]][:,detectorIndex])/300./60., label = '%d' %(detectorIndex+1))

plt.legend()
plt.xlabel('Bin');
plt.ylabel('Count/x-ray pulse')

plt.yscale('log')
plt.axis((0, 150, 1e-5, 20))
